﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IBLL;


namespace Skyworth.CRM.Web.SalesManagement
{
    public partial class AdjustSpecialQuery :BasePage
    {
        protected int totalRecords = 0;
        protected int totalPages = 1;

        private IWeekLongBLL weekLongBLL = IocContainer.Instance.Resolve<IWeekLongBLL>();
        private IPricePolicyBLL policyBLL = IocContainer.Instance.Resolve<IPricePolicyBLL>();

        protected void Page_Load(object sender, EventArgs e)
        {
            SystemOverTime();
            if (!Page.IsPostBack)
            {
                // 按钮权限控制
                ControlAuthority(new List<WebControl> { btnSearch }, 15);
                BindddlPricePolicy();
            }
            Query();
        }

        private void BindddlPricePolicy()
        {

            string strWhere = " where 1=1 ";
            Hashtable parameter = new Hashtable();
            DataTable dtPP = policyBLL.Query(strWhere, parameter);
            ddlPricePolicy.DataSource = dtPP;
            ddlPricePolicy.DataTextField = "PolicyName"; //dropdownlist的Text的字段
            ddlPricePolicy.DataValueField = "PolicyId";//dropdownlist的Value的字段
            ddlPricePolicy.DataBind();
        }


        protected void Query()
        {
            int pageIndex = 0;
            int.TryParse(this.pageNum.Value, out pageIndex);
            int pagePerSize = 0;
            int.TryParse(this.numPerPageSelect.Value, out pagePerSize);

            StringBuilder strWhere = new StringBuilder(" where 1=1  and SpecialType=0 ");
            if (!string.IsNullOrEmpty(this.txtName.Text))
            {
                strWhere.Append(" and SchemeName like '%" + this.txtName.Text + "%'");
            }
            if (!string.IsNullOrEmpty(this.txtCode.Text))
            {
                strWhere.Append(" and SchemeCode like '%" + this.txtCode.Text + "%'");
            }

            if (!string.IsNullOrEmpty(this.txtAdjustCode.Text))
            {
                strWhere.Append(" and FNumber like '%" + this.txtAdjustCode.Text + "%'");
            }

            if (!string.IsNullOrEmpty(this.txtStartDate.Text))
            {
                strWhere.Append(" and a.FBeginDate>='" + this.txtStartDate.Text + "' and a.FEndinDate<'" + Convert.ToDateTime(this.txtEndDate.Text).AddDays(1).ToString() + "'");
            }

            //if (!string.IsNullOrEmpty(this.txtAdjustCode.Text))
            //{
            strWhere.Append(" and  d.PolicyId=" + this.ddlPricePolicy.SelectedValue + " ");
            //}


            StringBuilder strOrderBy = new StringBuilder(" Order by  ");
            strOrderBy.Append(!string.IsNullOrEmpty(this.orderField.Value) ? " " + this.orderField.Value : "  SchemeName  ");
            strOrderBy.Append(!string.IsNullOrEmpty(this.orderValue.Value) ? " " + this.orderValue.Value : "  asc ");


             totalRecords = 0;
             totalPages = 0;
            DataTable dtProducts = weekLongBLL.GetSpecialByPage(strWhere.ToString(), strOrderBy.ToString(), pageIndex, pagePerSize, out totalRecords,
                                           out totalPages);

            this.EmpTableBody.Text = BuildEmployeeTable(dtProducts);
        }

        protected string BuildEmployeeTable(DataTable dtEmployee)
        {
            StringBuilder result = new StringBuilder();
            if (dtEmployee != null && dtEmployee.Rows.Count > 0)
            {
                int rowNum = 1;
                foreach (DataRow row in dtEmployee.Rows)
                {
                    result.Append(rowNum % 2 == 0 ? "<tr class='trbg' rel='" + row["FProductid"] + "'>" : "<tr rel='" + row["FProductid"] + "'>");
                    result.AppendFormat("<td>{0}</td>", row["FNumber"]);
                    result.AppendFormat("<td>{0}</td>", row["SchemeCode"]);
                    result.AppendFormat("<td>{0}</td>", row["SchemeName"]);
                    result.AppendFormat("<td>{0}</td>", row["ProductName"]);
                    result.AppendFormat("<td>{0}</td>", row["FOLDPrice"]);
                    result.AppendFormat("<td>{0}</td>", row["FNEWPrice"]);
                    result.AppendFormat("<td>{0}</td>", row["FBeginDate"]);
                    result.AppendFormat("<td>{0}</td>", row["FEndinDate"]);
                    result.AppendFormat("<td>{0}</td>", row["Fdate"]);
                    result.AppendFormat("<td>{0}</td>", row["FisSetName"]);
                    result.AppendFormat("<td>{0}</td>", row["FSetQty"]);
                    result.Append("</tr>");
                    rowNum++;
                }
            }
            return result.ToString();

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.pageNum.Value = "1";
            Query();
        }

        protected void ddlPricePolicy_SelectedIndexChanged(object sender, EventArgs e)
        {
            Query();
        }

    }
}
